﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DevComponents.DotNetBar;

namespace PlanningGIS.Win.Controls
{
    /// <summary>
    /// 进度条界面
    /// </summary>
    internal partial class FrmProgress : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmProgress()
        {
            InitializeComponent();
            ProgressType = zjgisProgressType.Circle;
            Max = 100;
            StepOne = 1;
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmProgress_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        public zjgisProgressType ProgressType { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int Max { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int StepOne { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        public void Start()
        {
            Start(ProgressType, Max, StepOne);
        }

        /// <summary>
        /// 启动进度条
        /// </summary>
        /// <param name="type"></param>
        /// <param name="max"></param>
        /// <param name="step"></param>
        public void Start(zjgisProgressType type, int max, int step = 1)
        {
            ProgressType = type;
            this.lblMsg.Visible = !(type == zjgisProgressType.Circle);
            this.progressBar.Visible = !(type == zjgisProgressType.Circle);
            this.pictureBox1.Visible = (type == zjgisProgressType.Circle);
            if (type == zjgisProgressType.Circle)
            {
                this.Size = new Size(850, 460);
            }
            else if (type == zjgisProgressType.Standard)
            {
                this.Size = new Size(280, 100);
                progressBar.ProgressType = eProgressItemType.Standard;
            }
            else if (type == zjgisProgressType.Marquee)
            {
                this.Size = new Size(280, 100);
                progressBar.ProgressType = eProgressItemType.Marquee;
            }
            progressBar.Maximum = max;
            progressBar.Minimum = 0;
            progressBar.Step = step;
            this.Show(Owner);
        }

        /// <summary>
        /// 根据step的值递增
        /// </summary>
        public void StepValue()
        {
            if (ProgressType == zjgisProgressType.Circle)
            {
                
            }
            else if (ProgressType == zjgisProgressType.Standard)
            {
                progressBar.PerformStep();
            }
            else if (ProgressType == zjgisProgressType.Marquee)
            {

            }
        }

        /// <summary>
        /// 递增到指定的值
        /// </summary>
        /// <param name="value"></param>
        public void StepTo(int value)
        {
            if (ProgressType == zjgisProgressType.Circle)
            {
                
            }
            else if (ProgressType == zjgisProgressType.Standard)
            {
                progressBar.Value = value;
            }
            else if (ProgressType == zjgisProgressType.Marquee)
            {

            }
        }

        /// <summary>
        /// 根据指定的值value递增
        /// </summary>
        /// <param name="value"></param>
        public void Increment(int value)
        {
            if (ProgressType == zjgisProgressType.Circle)
            {
                //circularProgress.Value += value;
                //if (circularProgress.Value >= circularProgress.Maximum)
                //    circularProgress.Value = 0;
            }
            else if (ProgressType == zjgisProgressType.Standard)
            {
                progressBar.Value += value;
            }
            else if (ProgressType == zjgisProgressType.Marquee)
            {

            }
        }

        /// <summary>
        /// 停止进度条
        /// </summary>
        public void Stop()
        {
            if (ProgressType == zjgisProgressType.Circle)
            {
                //circularProgress.IsRunning = false;
            }
            else if (ProgressType == zjgisProgressType.Standard)
            {

            }
            else if (ProgressType == zjgisProgressType.Marquee)
            {

            }
            this.Close();
        }

        /// <summary>
        /// 得到时间差的表述文字
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="ratio"></param>
        /// <returns></returns>
        private string GetTimeString(DateTime start, DateTime end, double ratio)
        {
            bool blnTemp = false;
            DateTime dtmT1 = start;
            DateTime dtmT2 = end;
            double day = dtmT2.Day - dtmT1.Day;
            double hour = dtmT2.Hour - dtmT1.Hour;
            double minute = dtmT2.Minute - dtmT1.Minute;
            double second = dtmT2.Second - dtmT1.Second;
            if (day < 0)
            {
                blnTemp = true;
            }
            else if ((day == 0) && (hour < 0))
            {
                blnTemp = true;
            }
            else if ((day == 0) && (hour == 0) && (minute < 0))
            {
                blnTemp = true;
            }
            else if ((day == 0) && (hour == 0) && (minute == 0) && (second < 0))
            {
                blnTemp = true;
            }
            if (blnTemp)
            {
                dtmT1 = end;
                dtmT2 = start;
            }
            day = dtmT2.Day - dtmT1.Day;
            hour = dtmT2.Hour - dtmT1.Hour;
            minute = dtmT2.Minute - dtmT1.Minute;
            second = dtmT2.Second - dtmT1.Second;
            if (second < 0)
            {
                second = second + 60;
                minute = minute - 1;
            }
            if (minute < 0)
            {
                minute = minute + 60;
                hour = hour - 1;
            }
            if (hour < 0)
            {
                hour = hour + 24;
                day = day - 1;
            }
            if (ratio > -1 && ratio < -1)
            {
                day = day * ratio;
                hour = hour * ratio;
                minute = minute * ratio;
                second = second * ratio;
            }
            int lngMode = (int)(second / 60);
            if (lngMode > 0)
            {
                minute = minute + lngMode;
                second = second - lngMode * 60;
            }
            lngMode = (System.Int32)(minute / 60);
            if (lngMode > 0)
            {
                hour = hour + lngMode;
                minute = minute - lngMode * 60;
            }
            lngMode = (System.Int32)(hour / 24);
            if (lngMode > 0)
            {
                day = day + lngMode;
                hour = hour - lngMode * 24;
            }
            string strTime = "";
            if (day > 0)
            {
                strTime = ((System.Int32)day).ToString() + "天";
            }
            if (day > 0 || hour > 0)
            {
                strTime = strTime + ((System.Int32)hour).ToString() + "时";
            }
            if (hour > 0 || minute > 0)
                strTime = strTime + ((System.Int32)minute).ToString() + "分";
            strTime = strTime + ((System.Int32)second).ToString() + "秒";
            return strTime;
        }
    }

}
